<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
        <el-main>
          <el-card class="box-card">
            <div slot="header" class="clearfix" style="line-height: 30px">
              <span><i class="el-icon-s-unfold"></i> 服饰优惠列表</span>
              <el-row style="float: right">
              <el-col>
                <el-button-group>
                  <el-button type="primary" icon="el-icon-plus" 
                    size="small" @click="handleAdd"
                  >新增优惠</el-button>
                </el-button-group>
              </el-col>
              </el-row>
            </div>
            <el-row>
            <el-col>
            <div class="text-item">
              <el-table v-loading="loading" :data="discountList">
                <el-table-column label="商品编号" align="center" prop="clId" />
                <el-table-column label="服饰名称" align="center" prop="clName" />
                <el-table-column label="服饰优惠" align="center" prop="discount" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
                  <template slot-scope="scope">
                    <el-button
                      size="small"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      >修改</el-button>
                    <el-button
                      size="small"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      >删除</el-button>
                 </template>
                </el-table-column>
              </el-table>

              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[5, 10, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                align="center"
              >
              </el-pagination>
            </div>
            </el-col>
            </el-row>
          </el-card>

        </el-main>
      </el-col>
    </el-row>

    
    <!-- 添加修改优惠信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="dlgcate=='ADD'" label="服饰编号" prop="clId">
          <el-input v-model="form.clId" placeholder="请输入服饰编号" />
        </el-form-item>
        <el-form-item label="服饰优惠" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入服饰优惠" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .clearfix {
    overflow: auto;
  }
  .el-header{
    background-color: #ffffff;
    color: #333;
    width: 100%;
    text-align: left;
    line-height: 50px;
  }

</style>

<script>

import { listDiscountByPage, addDiscount, updateDiscount, deleteDiscount } from "@/api/discount";
export default {
   name: 'Discount',
   data(){
    return{
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 当前页
      pageNum: 1,
      // 每页数据量
      pageSize: 5,
/*       // 服饰表格数据
      clothesList: [], */
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dlgcate: 'ADD',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,/* 
        clId: undefined,
        discount: undefined, */
      },

      upformclId:undefined,
      // 表单参数
      form: {
        clId: undefined,
        clName: undefined,
        discount: undefined,
      },
    }
  },

  created() {
    this.getDiscountList();
  },

  methods: {

    /** 查询服饰列表 */
    getDiscountList() {
      this.loading = true;
      listDiscountByPage(this.queryParams).then((response) => {
        this.discountList = response.data.list;
        this.total = response.data.total;
        this.pageNum = response.data.pageNum;
        this.pageSize = response.data.pageSize;
        this.loading = false;
      })
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getDiscountList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getDiscountList();
    },

    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.resetForm();
    },

    /** 表单重置 */
    resetForm() {
      this.form = {
        clId: undefined,
        clName: undefined,
        discount: undefined
      };
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.dlgcate = 'ADD';
      this.open = true;
      this.title = "添加服饰优惠";
      
    },

    /** 修改按钮操作 */
    handleUpdate(row) {/* 
      getdiscountById(row.clId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服饰";
      }); */

      this.upformclId = row.clId;
      
      this.dlgcate = 'UPD';
      this.open = true;
      this.title = "修改服饰";
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "修改服饰") {
            updateDiscount(this.upformclId, this.form.discount);
            this.open = false;
            this.title = "";
            this.$message.success('修改成功'); //后端返回消息字符串
            this.getDiscountList();
          } else {
            addDiscount(this.form.clId, this.form.discount).then((response) => {
              const flag = response.data;
              if(flag){
                this.$message.success('添加成功');
              }else{
                this.$message.error('操作失败');
              }
            });
            this.open = false;
            this.title = "";
            this.getDiscountList();
          }
        }
      });
      this.getDiscountList();
    }, 

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除' + row.clName+'？',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
       }
      )
      .then(function () {
        return deleteDiscount(row.clId);
      })
      .then(() => {
        this.getDiscountList();
        this.$message.success("删除成功");
      })
      .catch(() => {});
    }
  }

    
   
}
</script>